﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Noria.CFC
{
    public partial class MainForm : Form
    {
        private string file;
        private bool saved;

        private MainForm()
        {
            InitializeComponent();
        }

        public MainForm(string file)
            : this()
        {
            this.file = file;

            this.saved = false;
            UpdateTitle();
        }

        private void UpdateTitle()
        {
            this.Text = new FileInfo(file).Name + (saved ? "" : " (Unsaved)");
        }

        private void Form_Load(object sender, EventArgs e)
        {
            mainTB.Text = AppUtil.ReadFile(file);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                AppUtil.SaveFile(file, mainTB.Text);

                saved = true;
                UpdateTitle();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save file due to " + ex.Message, "Save", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NativeMethods.ShellAboutW(this.Handle, "Noria Char Format Converter", null, IntPtr.Zero);
        }

    }
}
